<template>
  <div class="app-container app-container-compactness">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="add">新增</el-button>
      </el-col>
    </el-row>

    <el-row type="flex" justify="end">
      <el-popover placement="bottom" width="50" trigger="click">
        <el-checkbox-group v-model="checkedTableColumns">
          <el-checkbox
            v-for="(item, index) in colTableHeaderTit"
            :key="index"
            :label="item.label"
          />
        </el-checkbox-group>
        <el-button
          slot="reference"
          type="primary"
          style="margin-right: 10px;
          position: relative;
          bottom: 15px;"
        >设置显示字段
        </el-button>
      </el-popover>
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      class="table-wrap"
      :data="list"
      row-key="id"
      border
      fit
      header-cell-class-name="header-cell-color"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="45"
      />
      <el-table-column
        v-for="(item, index) in bindTableColumns"
        :key="`colTableHeaderTit_${index}`"
        class-name="allowDrag"
        show-overflow-tooltip
        :prop="tableHeaderTit[index].field"
        :label="tableHeaderTit[index].label"
        align="center"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="detailed(scope.row)"
          >明细
          </el-button>
          <el-button
            size="mini"
            type="success"
            plain
            @click="handleExport(scope.row)"
          >导出
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-table v-loading="loading" :data="list" size="mini">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column prop="last_month_number" label="上月结转" align="center" />
      <el-table-column prop="this_month_in_number" label="本月入库" align="center" />
      <el-table-column prop="this_month_out_number" label="本月出库" align="center" />
      <el-table-column prop="current_number" label="当前数目" align="center" />
      <el-table-column prop="current_Good_Qty" label="Good_Qty" align="center" />
      <el-table-column prop="current_Hold_Qty" label="Hold_Qty" align="center" />
      <el-table-column prop="current_NG_Qty" label="NG_Qty" align="center" />
      <el-table-column prop="statement_datetime" label="统计时间" align="center" width="200px" />
      <el-table-column prop="operator_name" label="操作人" align="center" />
      <el-table-column prop="is_all_product" label="是否全部商品" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="detailed(scope.row)">明细
          </el-button>
        </template>
      </el-table-column>
    </el-table> -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />
    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :form="form" @getList="getList" />
    <!-- 盘点明细-->
    <Detailed :open.sync="open1" :title="title1" :statement_id="statement_id" @getList="getList" />
  </div>
</template>

<script>
import { statement, exportStatement } from '@/api/stock'
import Detailed from './Detailed'
import AddEdit from './AddEdit'
import Sortable from 'sortablejs'

export default {
  name: 'StockProductInventory',
  components: {
    AddEdit,
    Detailed
  },
  data() {
    return {
      tableHeaderTit: [
        { label: 'ID', field: 'id', isShow: true, minWidth: 50 },
        { label: '上月结转', field: 'last_month_number', isShow: true, minWidth: 50 },
        { label: '客户业务单号', field: 'customer_business_number', isShow: true, minWidth: 50 },
        { label: '本月入库', field: 'this_month_out_number', isShow: true, minWidth: 50 },
        { label: '本月出库', field: 'current_number', isShow: true, minWidth: 50 },
        { label: 'Good_Qty', field: 'current_Good_Qty', isShow: true, minWidth: 50 },
        { label: 'Hold_Qty', field: 'current_Hold_Qty', isShow: true, minWidth: 50 },
        { label: 'NG_Qty', field: 'current_NG_Qty', isShow: true, minWidth: 50 },
        { label: '统计时间', field: 'statement_datetime', isShow: true, minWidth: 50 },
        { label: '操作人', field: 'operator_name', isShow: true, minWidth: 50 },
        { label: '是否全部商品', field: 'is_all_product', isShow: true, minWidth: 50 }
      ],
      colTableHeaderTit: [
        { label: 'ID', field: 'id', isShow: true, minWidth: 50 },
        { label: '上月结转', field: 'last_month_number', isShow: true, minWidth: 50 },
        { label: '客户业务单号', field: 'customer_business_number', isShow: true, minWidth: 50 },
        { label: '本月入库', field: 'this_month_out_number', isShow: true, minWidth: 50 },
        { label: '本月出库', field: 'current_number', isShow: true, minWidth: 50 },
        { label: 'Good_Qty', field: 'current_Good_Qty', isShow: true, minWidth: 50 },
        { label: 'Hold_Qty', field: 'current_Hold_Qty', isShow: true, minWidth: 50 },
        { label: 'NG_Qty', field: 'current_NG_Qty', isShow: true, minWidth: 50 },
        { label: '统计时间', field: 'statement_datetime', isShow: true, minWidth: 50 },
        { label: '操作人', field: 'operator_name', isShow: true, minWidth: 50 },
        { label: '是否全部商品', field: 'is_all_product', isShow: true, minWidth: 50 }
      ],
      list: [],
      nimabi: false,
      queryParams: {
        page: 1,
        limit: 10,
        goods_name: undefined,
        warehousing_id: undefined
      },
      loading: false,
      total: 0,
      open: false,
      open1: false,
      title: '',
      title1: '',
      statement_id: '',
      form: {}
    }
  },
  computed: {
    bindTableColumns() { // 表格展示的字段
      const res = this.colTableHeaderTit.filter((v) => {
        return v.isShow == true
      })
      this.tableHeaderTit = JSON.parse(JSON.stringify(res))
      return res
    },
    checkedTableColumns: {
      get() {
        return this.bindTableColumns.map((v) => v.label)
      },
      set(value) {
        this.nimabi = false
        this.colTableHeaderTit.forEach((v) => {
          if (value.includes(v.label)) {
            v.isShow = true
          } else {
            v.isShow = false
          }
        })
      }
    }
  },
  mounted() {
    this.columnDrop() // 可拖拽列
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    async getList() {
      const that = this
      this.loading = true
      statement(this.queryParams).then(res => {
        if (res.code === 10000) {
          that.list = res.data
          that.total = res.count
          that.list.forEach(item => {
            if (item.is_all_product == 0) {
              item.is_all_product = '全部商品'
            } else {
              item.is_all_product = '部分商品'
            }
          })
          console.log(that.list)
          that.loading = false
        }
      })
    },
    columnDrop() {
      var _this = this
      const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
        draggable: '.allowDrag', // 允许拖拽元素(el-table-column上设置class-name为允许拖拽)
        animation: 180,
        delay: 0,
        // 之前调用onEnd方法会出现表格DOM不更新以及表头对不上的情况所以更换为onUpdate方法
        // 参考资料 https://www.jianshu.com/p/fd6eb408d8bd
        onUpdate: function(evt) {
          // 修改items数据顺序
          var newIndex = evt.newIndex
          var oldIndex = evt.oldIndex
          const newItem = wrapperTr.children[newIndex]
          const oldItem = wrapperTr.children[oldIndex]

          // 先删除移动的节点
          wrapperTr.removeChild(newItem)
          // 再插入移动的节点到原有节点，还原了移动的操作
          if (newIndex > oldIndex) {
            wrapperTr.insertBefore(newItem, oldItem)
          } else {
            wrapperTr.insertBefore(newItem, oldItem.nextSibling)
          }
          // 更新items数组(index-1是第一列有一个多选列不支持拖拽,否则会有排序错乱的问题)
          var item = _this.tableHeaderTit.splice(oldIndex - 1, 1)
          _this.tableHeaderTit.splice(newIndex - 1, 0, item[0])
        }

      })
    },
    handleSelectionChange(selection) {
      console.log('拖拽')
      this.selectionArr = selection
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    add() {
      this.form = {}
      this.open = true
      this.title = '添加'
    },
    /** 明细 */
    detailed(row) {
      console.log(row)
      this.open1 = true
      this.title1 = row.year_text + '年' + row.month_text + '月' + row.day_text + '日库存管理统计表'
      this.statement_id = String(row.id)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        goods_name: undefined
      }
      this.handleQuery()
    },
    /** 导出 */
    handleExport(row) {
      exportStatement({ statement_id: row.id }).then(res => {
        if (res.code === 10000) {
          var url = res.data.url.replace('127.0.0.1:8080', 'cross.qekj.net')
          window.open(url)
          this.$message.success('导出成功')
        }
      })
    }
  }
}
</script>

<style scoped></style>
